@include('admin.layout.bone_header')


<div class="layui-btn-container">
    <button class="layui-btn" data-type="addCategory">添加分类</button>

</div>

<table class="layui-hide" id="think-table" lay-filter="think-table"></table>


<script id="barDemo" type="text/html">
        <a class="layui-btn layui-btn-xs" lay-event="category_edit">编辑</a>

</script>

<script>

    layui.use('table', function () {
        let table = layui.table;
        UI.getPageTable(table, "{{route('admin.category.lists')}}", [
            {field: 'category_id', title: 'ID', width:80, sort: true,fixed: 'left'}
            ,{field: 'category_name',title:'分类名称'}
            ,{field: 'created_at',title:'分类创建时间',templet:function (d){
                    console.log(d['created_at']);
                    return layui.util.toDateString(parseInt(d['created_at'])*1000,'yyyy-MM-dd HH:mm:ss')
                }}
            ,{fixed: 'right',title:'操作', align:'center', toolbar: '#barDemo'}
        ]);

        table.on('tool(think-table)', function (obj) {
            let data = obj.data;
            if(obj.event == 'category_edit'){
                UI.openLayer("{{route('admin.category.update')}}?category_id="+data['category_id']);
            }
        });
        let $ = layui.$, active = {
            addCategory:function (){
              UI.openLayer("{{route('admin.category.add')}}");
            },
            reload:function (){
                let search_key = $('#search_key');
                //执行重载
                table.reload('think-table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        'search_key':search_key.val(),
                    }
                });
            }
        };
        $('.layui-btn-container .layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('.search .layui-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    })


</script>


@include('admin.layout.bone_footer')
